package com.itheima.reggie.Impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.itheima.reggie.EmployeeService;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.domain.Employee;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public ResultInfo login(String username, String password) {
        System.out.println(username+""+password);
        //1. 调用mapper,根据username查询;如果查询不到,返回错误提示
        Employee employee = employeeMapper.findByUsername(username);
        if (employee == null) {
            return ResultInfo.error("当前用户不存在,登录失败");
        }
        //将传入的密码加密和查询的密码对比
        String s = SecureUtil.md5(password);

        System.out.println("s = " + s);
        if (!StringUtils.equals(s,employee.getPassword())) {
            return ResultInfo.error("当前密码不存在,登录失败");
        }
        //查看用户的ststus是否是禁用状态
        if (employee.getStatus()== Employee.STATUS_DISABLE) {
            return ResultInfo.error("当前用户是禁用状态");
        }
        //条件都对返回一条信息
        System.out.println("0000000000000000000000");
        return ResultInfo.success(employee);
    }

    @Override
    public List<Employee> findByName(String name) {

        return employeeMapper.findByName(name);
    }

    @Override
    public void save(Employee employee) {
        //设置默认密码加密
        employee.setPassword(SecureUtil.md5("12345"));
        //状态
        employee.setStatus(Employee.STATUS_ENABLE);
        //调用mapper新增
        employeeMapper.insert(employee);
    }

    @Override
    public Employee findById(Long id) {
      return employeeMapper.findById(id);
    }

    @Override
    public void update(Employee employee) {
        //调用mapper
        employeeMapper.updateById(employee);
    }
}
